const Router = require('@koa/router')
const router = new Router()

const db = require('../db')
const { brand } = require('../vri')

// 获取品牌列表
router.get('/search', async (ctx) => {
  const { query } = ctx
  const { adopt, error, value } = brand.search.verifies(query)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  const { qw, page, size } = value
  const where = {
    name: qw
      ? {
          op: 'LIKE',
          val: qw,
        }
      : undefined,
  }
  const data = await db.find({
    form: 'brand',
    where,
    limit: page
      ? {
          page,
          size,
        }
      : undefined,
  })
  const total = await db.count({
    form: 'brand',
    where,
  })
  ctx.body = {
    code: 200,
    data,
    total,
  }
})

// 从id获取品牌
router.get('/fromid', async (ctx) => {
  const { query } = ctx
  const { adopt, error, value } = brand.fromid.verifies(query)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  const data = await db.first({
    form: 'brand',
    virtual: ['classifyList'],
    where: {
      id: value.id,
    },
  })
  if (!data) return ctx.error(431)
  ctx.body = {
    code: 200,
    data,
  }
})

// 添加品牌
router.post('/add', async (ctx) => {
  // 登陆验证
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  const { body } = ctx.request
  const { adopt, error, value } = brand.add.verifies(body)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  const { lastId: id } = await db.insert({
    form: 'brand',
    value,
    lastId: true,
  })
  ctx.body = {
    code: 200,
    msg: '添加成功',
    id,
  }
})

// 修改品牌
router.post('/modify', async (ctx) => {
  // 登陆验证
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  const { body } = ctx.request
  const { adopt, error, value } = brand.modify.verifies(body)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  const { id } = value
  delete value.id
  const { affectedRows } = await db.update({
    form: 'brand',
    where: {
      id,
    },
    set: value,
  })
  if (!affectedRows) return ctx.error(431)
  ctx.body = {
    code: 200,
    msg: '成功',
  }
})

// 删除
router.post('/del', async (ctx) => {
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  const { body } = ctx.request
  const { adopt, error, value } = brand.del.verifies(body)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  const { affectedRows } = await db.delete({
    form: 'brand',
    where: {
      id: value.id,
    },
  })
  if (!affectedRows) return ctx.error(431)
  return (ctx.body = {
    code: 200,
    msg: '成功',
  })
})

// 批量删除
router.post('/dels', async (ctx) => {
  const { user } = ctx.state
  if (!user) return ctx.error('401')
  const { body } = ctx.request
  const { adopt, error, value } = brand.dels.verifies(body)
  if (!adopt) return ctx.error(430, { error: error.toString() })
  const { affectedRows } = await db.delete({
    form: 'brand',
    where: {
      id: value.ids,
    },
  })
  if (!affectedRows) return ctx.error(431)
  return (ctx.body = {
    code: 200,
    msg: '成功',
  })
})

module.exports = router.routes()
